﻿import { AfterViewInit, Component, Injector, ViewEncapsulation, OnInit } from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import {
    TenantDashboardServiceProxy,
    DashAnnualOutput,
    DashModelOutput,
    DashWorkshopOutput
} from '@shared/service-proxies/service-proxies';
import { curveBasis } from 'd3-shape';
import G2 from '@antv/g2/build/g2';

import * as _ from 'lodash';

class CostChart3  {
    workshopData = new Array<DashWorkshopOutput>();
    selectedWorkshopData = new DashWorkshopOutput();

    customColors = [
        { name: '车间收入', value: '#00c5dc' },
        { name: '收入', value: '#0fcca6' },
        { name: '支出', value: '#eee' },
        { name: '原材料成本', value: '#4397f1' },
        { name: '辅助材料成本', value: '#9E7AD9' },
        { name: '标准件成本', value: '#F168A7' },
        { name: '外协产品成本', value: '#EFA262' },
        { name: '工装成本', value: '#6CCF83' },
        { name: '燃动成本', value: '#47B9CB' },
        { name: '人工成本', value: '#6E7BDF' },
        { name: '制造成本', value: '#7A92A8' },
        { name: '其他直接成本', value: '#E1886C' }
    ];

    curve: any = curveBasis;

    constructor(private _dashboardService: TenantDashboardServiceProxy) {
    }

    init() {
        this.reload();
    }

    formatData(): any {
        for (let j = 0; j < this.workshopData.length; j++) {
            let result1: any = this.workshopData[j].result1;

            result1.changeData = [
                {
                    name: '车间收入',
                    series: result1.data
                }
            ];

            let result2 = this.workshopData[j].result2;

            result2.data.forEach(el => {
                let sum = _.sum(el.value);
                let avg = sum / el.value.length;

                let changeData = [
                    {
                        name: el.name,
                        series: el.value.map((el1, idx) => {
                            return { name: idx + 1, value: el1 };
                        })
                    }
                ];

                (el as any).sum = sum;
                (el as any).avg = avg;
                (el as any).changeData = changeData;
            });
        }
    }

    reload() {
        this._dashboardService.getDashWorkshop().subscribe(result => {
            this.workshopData = result;
            this.selectedWorkshopData = this.workshopData.length > 0 ? this.workshopData[0] : new DashWorkshopOutput();
            this.formatData();
        });
    }
}
class CostChart5 {
    constructor(private _dashboardService: TenantDashboardServiceProxy) {
    }
    productChange = [];

    productValue = [];

    init() {
        this.reload();
    }

    reload() {
        this._dashboardService.getWorkshopDashChange(51).subscribe(result => {
            this.productChange = result.productChanges;
            this.productValue = result.productValues;
        });
    }
}

@Component({
    templateUrl: './workshop-dashboard.component.html',
    styleUrls: ['./workshop-dashboard.component.less'],
    encapsulation: ViewEncapsulation.None,
    animations: [appModuleAnimation()]
})
export class WorkshopDashboardComponent extends AppComponentBase implements OnInit {
    costChart3: CostChart3;
    costChart5: CostChart5;

    constructor(injector: Injector, private _dashboardService: TenantDashboardServiceProxy) {
        super(injector);
        this.costChart3 = new CostChart3(this._dashboardService);
        this.costChart5 = new CostChart5(this._dashboardService);
    }

    ngOnInit(): void {
        this.costChart3.init();
        this.costChart5.init();
    }

    changeWorkshop(item: DashWorkshopOutput) {

        this.costChart3.selectedWorkshopData = item;

        this.costChart5.reload();
    }

}
